//
//  BKPopoverButton.m
//  baokaodaxue
//
//  Created by SKar on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKPopoverButton.h"

#define POPWIDTH 150
#define POPHEIGHT 280

@interface BKPopoverButton () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign, getter=isPopovered) BOOL popovered;
@property (nonatomic, strong) UITableViewController *childViewController;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIButton *cancelButton;

@end

@implementation BKPopoverButton

- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
    
    self = [super awakeAfterUsingCoder:aDecoder];
    if (self) {
        
        [self addTarget:self action:@selector(popoverMenu) forControlEvents:UIControlEventTouchUpInside];
        self.popovered = NO;
        
    }
    return self;
    
}

- (void)dealloc {
    _childViewController.tableView.dataSource = nil;
    _childViewController.tableView.delegate = nil;
}

- (UITableViewController *)childViewController {
    if (!_childViewController) {
        _childViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        _childViewController.tableView.dataSource = self;
        _childViewController.tableView.delegate = self;
    }
    return _childViewController;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:_delegate.view.bounds];
    }
    return _backgroundView;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _cancelButton;
}

- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    dispatch_async(dispatch_get_main_queue(), ^{
        [_childViewController.tableView reloadData];
    });
}

- (void)popoverMenu {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (!self.isPopovered) {
            [self.delegate addChildViewController:self.childViewController];
            CGRect buttonFrame = [self.superview convertRect:self.frame toView:self.delegate.view];
            if (buttonFrame.origin.x + POPWIDTH > self.delegate.view.frame.size.width) {
                buttonFrame.origin.x = buttonFrame.origin.x + buttonFrame.size.width - POPWIDTH;
            }
            [self.childViewController.view setFrame:CGRectMake(buttonFrame.origin.x, buttonFrame.origin.y + buttonFrame.size.height, POPWIDTH, POPHEIGHT)];
            [self.delegate.view addSubview:self.childViewController.view];
            [self.childViewController didMoveToParentViewController:self.delegate];
            
            [self.delegate.view insertSubview:self.backgroundView belowSubview:self.childViewController.view];
            [self.cancelButton setFrame:[self.superview convertRect:self.frame toView:self.delegate.view]];
            [self.cancelButton addTarget:self action:@selector(popoverMenu) forControlEvents:UIControlEventTouchUpInside];
            [self.delegate.view addSubview:self.cancelButton];
            self.popovered = YES;
        } else {
            [self.childViewController willMoveToParentViewController:nil];
            [self.childViewController removeFromParentViewController];
            [self.childViewController.view removeFromSuperview];
            [self.backgroundView removeFromSuperview];
            [self.cancelButton removeFromSuperview];
            self.popovered = NO;
        }
        
    });
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.titleLabel.text = self.dataArray[indexPath.row];
    [self popoverMenu];
    self.completion();
}

@end
